package org.example.user.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.user.entity.Role;
import org.example.user.entity.User;
import org.example.user.entity.UserDto;
import org.example.user.service.IRoleService;
import org.example.user.service.IUserService;
import org.example.user.utils.MyJwtUtil;
import org.example.user.utils.ResultJson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-10-11
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    IRoleService roleService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/getUser")
    public UserDto getUser(Integer id) {
        User user = userService.getById(id);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        return userDto;
    }

    @GetMapping("/list")
    public List<UserDto> list() {
        List<User> list = userService.list();
        List<UserDto> userDtos=new ArrayList<>();
        for (User user : list) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(user,userDto);
            userDtos.add(userDto);
        }
        return userDtos;
    }

    @PostMapping("/login")
    public ResultJson login(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            return ResultJson.fail(203, "用户名密码不能为空", null);
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User loginUser = userService.getOne(wrapper);
        if (null == loginUser) {
            return ResultJson.fail(203, "用户名密码不能为空", null);
        }
        String pwdStr = SecureUtil.md5(user.getPassword() + "123");
        System.err.println("用户登录密码:"+pwdStr);
        if (!loginUser.getPassword().equals(pwdStr)) {
            return ResultJson.fail(203, "密码错误", null);
        }
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("id",loginUser.getRoleId());
        Role role = roleService.getOne(roleQueryWrapper);
        System.err.println(role);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(loginUser,userDto);
        System.err.println(userDto);
        userDto.setRoleName(role.getName());
        userDto.setRoleCode(role.getCode());

        System.err.println(userDto);

        String token = MyJwtUtil.createToken(userDto);
        redisTemplate.opsForValue().set(userDto.getUsername(),token,1, TimeUnit.HOURS);
        return ResultJson.success("登录成功", token);

    }
}
